package com.smzd.framework.system.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractUserOwnedController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.business.MeetingService;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.system.domain.User;
import com.smzd.framework.utils.MeetingSessionHolder;
import com.smzd.framework.utils.db.ORPage;

@Controller
@RequestMapping("/system/meetings")
@Access(Role.TAG_ROLE_MEETING)
public class MeetingController extends AbstractUserOwnedController<Meeting> {
    @Autowired
    private MeetingService service;

    @Override
    protected IFGService<Meeting> getService() {
        return service;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, Meeting elem, BindingResult result) throws Exception {
        User user = MeetingSessionHolder.getLoginUser();
        if (user.getType() == User.USER_TYPE_MEETING_OWNER) {
            Exception e = new Exception("不允许创建会务");
            throw e;
        }
        return super.toAddPage(model, elem, result);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, Meeting elem, BindingResult result) throws Exception {
        User user = MeetingSessionHolder.getLoginUser();
        if (user.getType() == User.USER_TYPE_MEETING_OWNER) {
            Exception e = new Exception("不允许创建会务");
            throw e;
        }
        return postAddData(elem);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, Meeting elem, BindingResult result) throws Exception {
        User user = MeetingSessionHolder.getLoginUser();
        if (user.getType() == User.USER_TYPE_MEETING_MANAGER) {
            model.put("_meeting_manager_role", true);
        }
        return super.toListPage(model, elem, result);
    }

    /**
     * 列出当前所有的meeting。<br>
     * 这个应该指出，列出来的meeting是当前meeting下所有的meetingHolder们所拥有的meeting，因此从数据传输流程到数据查询流程都应该变化。<br>
     * 当前的管理员，普通管理员也好，meetingHolder们也好，只要具备权限就应该能获得数据。<br>
     * 数据的指向应该是当前Meeting，查询当中可能需要增加一级的查询，就是meeting.owner.owner=Meeting
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String getListData(Map<String, Object> model, ORPage<User, Meeting> page, BindingResult result)
            throws Exception {
        Meeting meeting = MeetingSessionHolder.getMeeting();
        page.setId(meeting.getId());
        page.setOwner(MeetingSessionHolder.getLoginUser());
        return super.getListData(model, page, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, Meeting elem, BindingResult result) throws Exception {
        User user = MeetingSessionHolder.getLoginUser();
        if (user.getType() == User.USER_TYPE_MEETING_OWNER) {
            Exception e = new Exception("不允许修改会务");
            throw e;
        }
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, elem, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, Meeting elem, BindingResult result) throws Exception {
        User user = MeetingSessionHolder.getLoginUser();
        if (user.getType() == User.USER_TYPE_MEETING_OWNER) {
            Exception e = new Exception("不允许修改会务");
            throw e;
        }
        return super.postEditData(model, elem, result);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, Meeting elem, BindingResult result) throws Exception {
        return super.postRemoveData(model, elem, result);
    }

}
